package com.hzdl.car.contorlle;

import com.github.pagehelper.PageInfo;
import com.hzdl.car.bean.Menu;
import com.hzdl.car.service.MenuService;
import com.hzdl.car.util.HzdlResultJson;
import com.hzdl.car.vo.MenuVo;
import com.hzdl.car.vo.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("menu")
public class MenuContorlle {
    @Autowired
    private MenuService service;

    @RequestMapping("leftMenu")
    public HzdlResultJson left(){

        List<TreeNode> list = service.findByPid(0);
        return new HzdlResultJson(list);
    }
    @RequestMapping("right")
    public  HzdlResultJson right(MenuVo menuVo){
        PageInfo<Menu> pageInfo = service.findWhere(menuVo);
        return new HzdlResultJson(pageInfo.getTotal(),pageInfo.getList());
    }

    @RequestMapping("add")
    public HzdlResultJson add(MenuVo menuVo){
        int row = service.add(menuVo);
        if (row>0){
            return new HzdlResultJson("添加成功",null);
        }else {
            return new HzdlResultJson(-1,"添加失败");
        }


    }

    @RequestMapping("update")
    public HzdlResultJson update(MenuVo menuVo){
        int row = service.update(menuVo);
        if (row>0){
            return new HzdlResultJson("修改成功",null);
        }
        return new HzdlResultJson(-1,"修改失败");

    }
    @RequestMapping("exitsChildren")
    public  HzdlResultJson exitsChildren(MenuVo menuVo){
        int row = service.countByPid(menuVo.getId());
        if (row>0){
            return new HzdlResultJson("1");

        }else {
            return new HzdlResultJson("0");
        }
    }

    @RequestMapping("delete")
    public HzdlResultJson delete(MenuVo menuVo){
        int row = service.delete(menuVo.getId());
        if (row>0){
            return HzdlResultJson.DELETE_SUCCESS();
        }else{
            return HzdlResultJson.DELETE_FAIL();
        }
    }


}
